
/**
 * 接口请求封装
 * @author yangjinlai 20200513
 * 参数说明
 * opt:
 * * ****name:具体业务请求名称
 * * ****method:默认POST
 * * ****url:接口路径
 * * ****params或data:具体业务请求参数
 * * ****openLoading: 是否展示loading
 *
 * callback: 接口成功回调
 * callbackFail: 接口失败回调
 * callbackNoData：接口无数据时回调
 */


import _axios from "./axios";
// import tipAction from "@common/components/otherComponents/tipAction/tipAction"

//黑条提示
import { Toast } from 'mint-ui';
import { Indicator } from 'mint-ui';

//接口失败时调用的方法
function ajaxFail( callbackFail, response){

	//接口失败
	let message = response.data.message ? response.data.message : '系统开小差啦，请联系系统管理员';
	
	//有callbackFail就调用，没有的话黑条提示
	callbackFail ? callbackFail(response) :  Toast(message);
}

//计算接口数量
let num = 0;

let axiosHttp = {

    http(opt) {

            let opts = opt || {};
            if (!opts.url) {
                console.error('缺少请求地址');
                return false;
            }
            if (opts.method === undefined) opts.method = 'POST';

            num++;

            //接口请求之前，展示loading
            opts.openLoading && Indicator.open();

            //超过5s，如果因接口时间过长或是其他原因导致Loading未隐藏的
            //将loading隐藏
            setTimeout(function(){
                Indicator.close();
            }, 5000)

            let result = await 

            return _axios({
                method: opts.method,
                url: opts.url,
                params: opts.params || {},
                data: opts.data ? JSON.stringify(opts.data) : {},
                //timeout: 15000,
            })
    },

    axiosCallback: function( axiosPromise, opts, callback, callbackFail, callbackNoData ){
	
		axiosPromise.then(response => {
		    
		    if (opts.complete) opts.complete();

		    if(response.data.status == '0000'){
		    	//接口成功
		        callback && callback(response) 
		    }
		    else if (response.data.status == '1000'){
		        //操作成功，数据为空
		        callbackNoData && callbackNoData(response)
		    }
		    else{
		    	//接口失败
		    	ajaxFail( callbackFail, response)
		    }

		    num--;

		    if( num == 0){
		        Indicator.close();
		    }

		}, response => {

		    num--;

		    if( num == 0){
		        Indicator.close();
		    }

		    //接口失败
		    ajaxFail( callbackFail, response);

		    console.error((opts.name || '') + "请求失败");

		}).catch(response => {

		    num--;

		    if( num == 0){
		        Indicator.close();
		    }

		    console.error((opts.name || '') + "请求异常", response);
		   
		});
	}
}


export default axiosHttp
